package com.mingtu.guards.fragment;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseFragment;
import com.mingtu.common.bean.UserInfoBean;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.SPTools;
import com.mingtu.common.view.MultiGridView;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.guards.R;
import com.mingtu.guards.activity.AircraftMonitorActivity;
import com.mingtu.guards.activity.EcologyMonitorActivity;
import com.mingtu.guards.activity.InfraredMonitorActivity;
import com.mingtu.guards.activity.MonitoringModuleActivity;
import com.mingtu.guards.adapter.LotAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LotFragment extends BaseFragment implements LazyFragmentPagerAdapter.Laziable {


    @BindView(R.id.grid_view)
    MultiGridView gridView;
    @BindView(R.id.srl_up)
    SmartRefreshLayout srlUp;
    private LotAdapter lotAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_lot;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView(View view) {
        lotAdapter = new LotAdapter(getContext());
        gridView.setAdapter(lotAdapter);
        lotAdapter.setOnItemClickListener(new LotAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (position == 0) {
                    IntentUtils.getInstance().readyGo(getActivity(), EcologyMonitorActivity.class);
                } else if (position == 1) {
                    IntentUtils.getInstance().readyGo(getActivity(), InfraredMonitorActivity.class);
                } else if (position == 2) {
                    IntentUtils.getInstance().readyGo(getActivity(), MonitoringModuleActivity.class);
                } else if (position == 3) {
                    IntentUtils.getInstance().readyGo(getActivity(), AircraftMonitorActivity.class);
                }
            }
        });
    }


    @Override
    protected void getData() {
        initPullRefresh();//刷新
    }

    private void initPullRefresh() {
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                srlUp.setNoMoreData(false);
                getUserInfo();
            }
        });
    }


    /**
     * 获取用户信息
     */
    private void getUserInfo() {

        OkGo.<String>get(Constant.getInstance().GET_USER_INFO)
                .tag(this)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                UserInfoBean userInfoBean = new UserInfoBean();
                                userInfoBean = gson.fromJson(result, UserInfoBean.class);

                                UserInfoBean.UserBean userBean = userInfoBean.getUser();
                                List<String> isLeaderList = new ArrayList<>();
                                List<UserInfoBean.UserBean.RoleListBean> roleList = userBean.getRoleList();
                                if (roleList != null && roleList.size() > 0) {
                                    for (int i = 0; i < roleList.size(); i++) {
                                        String isLeader = roleList.get(i).getIsLeader(); //isLeader 1：职员 0：普通
                                        isLeaderList.add(isLeader);
                                    }

                                }

                                if (roleList != null && roleList.size() > 0) {
                                    UserInfoBean.UserBean.RoleListBean roleListBean = roleList.get(0);
                                    List<UserInfoBean.UserBean.RoleListBean.MenuAppListBean> menuAppListBean = roleListBean.getMenuAppList();
                                    if (menuAppListBean != null) {
                                        if (menuAppListBean.size() > 0) {
                                            List<String> typeList = new ArrayList<>();
                                            for (int i = 0; i < menuAppListBean.size(); i++) {
                                                String path = menuAppListBean.get(i).getPath();
                                                typeList.add(path);
                                            }
                                            //是否有下载图片、视频的权限
                                            if (typeList.contains("download")) {
                                                SPStaticUtils.put(SPTools.download, true);
                                            } else {
                                                SPStaticUtils.put(SPTools.download, false);
                                            }

                                        } else {
                                            SPStaticUtils.put(SPTools.download, false);
                                        }
                                    } else {
                                        SPStaticUtils.put(SPTools.download, false);
                                    }

                                }
                                //是否是领导权限
                                if (isLeaderList.contains("1")) {
                                    SPStaticUtils.put(SPTools.isLeader, true);
                                } else {
                                    SPStaticUtils.put(SPTools.isLeader, false);
                                }
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        MyLogUtil.e(getClass().getSimpleName(), "onError==" + result);
                    }
                });


    }

}